package com.dedaletechnology.mrquizz.server;

import com.dedaletechnology.mrquizz.model.MultipleChoiceQuestion;
import com.dedaletechnology.mrquizz.model.MultipleChoiceQuestions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

public class MultipleChoiceQuestionRepository implements MultipleChoiceQuestions {

    PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");

    /**
     * @see com.dedaletechnology.mrquizz.model.MultipleChoiceQuestions#getAll()
     */
    @Override
    public Collection<MultipleChoiceQuestion> getAll() {
        PersistenceManager pm = pmfInstance.getPersistenceManager();
        try {
            List<MultipleChoiceQuestion> questions = new ArrayList<MultipleChoiceQuestion>();
            Extent<MultipleChoiceQuestion> extent = pm.getExtent(MultipleChoiceQuestion.class, false);
            for (MultipleChoiceQuestion question : extent) {
                questions.add(question);
            }
            extent.closeAll();

            return questions;
        } finally {
            pm.close();
        }
    }

    /**
     * @see
     * com.dedaletechnology.mrquizz.model.MultipleChoiceQuestions#create(net.kindleit.gae.example.model.Message)
     */
    @Override
    public void create(MultipleChoiceQuestion question) {
        PersistenceManager pm = pmfInstance.getPersistenceManager();
        try {
            pm.makePersistent(question);
        } finally {
            pm.close();
        }
    }

    /**
     * @see
     * com.dedaletechnology.mrquizz.model.MultipleChoiceQuestions#deleteByIdQuestion(java.lang.Long)
     */
    @Override
    public void deleteByIdQuestion(Long idQuestion) {
        PersistenceManager pm = pmfInstance.getPersistenceManager();
        try {
            pm.deletePersistent(pm.getObjectById(MultipleChoiceQuestion.class, idQuestion));
        } finally {
            pm.close();
        }
    }
}
